document.ready(function() {
    openLoad();

    setValue('infoid', getUrlParam('infoid'));
    setValue('infoType', getUrlParam('m'));
    setValue('verifyType', getUrlParam('type'));

    if (getValue('verifyType') == 'company') {
        setHtml('title', '商家认证');
    }

    setDisplay('mainBg', '');
    closeLoad();
});

function back() {
    parent.closeSlipIframeDiv();
}

function openCompanyBar(e) {
    var e = e || window.event;
    if (e.keyCode == 13) {
        if (isEmpty('keyword') || isWicked('keyword')) {
            showTip('请填写正确的关键词');
            return;
        }

        //提示【正在加载中】
        showFloatlistBar('loading', 'keyword');

        var param = {};
        param.ctrl = 'verify';
        param.func = 'getCompanyList';
        param.keyword = getAjaxValue('keyword');

        sendAjax(param, function(result) {
            loadFloatlist([
                { width:'auto', valueid:'name', click:'selectCompany', clickParam:'verifyid,code,name,pic' },
                { width:'regular', valueid:'code', color:'#b1b2b3' }
            ], result.companyList, 'keyword');
        });
    }
}

function selectCompany(verifyid, code, name, pic) {
    setValue('oldVerifyid', verifyid);
    setValue('oldLicense', code);
    setValue('oldBusiness', name);
    getItem('oldLicensePic').src = pic;

    setDisplay('oldBar', '');
    closeFloatlistBar();
}

function selectVerify() {
    if (isEmpty('oldVerifyid')) {
        showTip('认证编号格式异常');
        return;
    }
    if (isEmpty('oldBusiness') || isWicked('oldBusiness')) {
        showTip('商家名称格式异常');
        return;
    }

    var data = getFieldAndTable();
    if (data == '') {
        showTip('传入的信息类型有误');
        return;
    }

    var param = {};
    param.ctrl = 'verify';
    param.func = 'selectVerify';
    param.infoid = getAjaxValue('infoid');
    param.verifyid = getAjaxValue('oldVerifyid');
    param.business = getAjaxValue('oldBusiness');
    param.table = data.split('@')[0];
    param.idfield = data.split('@')[1];

    sendAjax(param, function(result) {
        submitRecord('edit', '认证页-选取往期记录', '商家认证', getValue('infoid'));
        parent.closeSlipIframeDiv();
        parent.queryKey('page');
    });
}

function saveCompany() {
    if (isEmpty('business') || isWicked('business')) {
        showTip('商家名称格式异常');
        return;
    }
    if (isEmpty('license')) {
        showTip('统一信用代码格式异常');
        return;
    }
    if (files == '') {
        showTip('请上传营业执照');
        return;
    }
    if (!isEmpty('remark') && isWicked('remark')) {
        showTip('备注格式异常');
        return;
    }

    var data = getFieldAndTable();
    if (data == '') {
        showTip('传入的信息类型有误');
        return;
    }

    var param = {};
    param.ctrl = 'verify';
    param.func = 'saveCompany';
    param.infoid = getAjaxValue('infoid');
    param.business = getAjaxValue('business');
    param.license = getAjaxValue('license');
    param.licensepic = encodeURIComponent(files);
    param.remark = getAjaxValue('remark');
    param.table = data.split('@')[0];
    param.idfield = data.split('@')[1];

    sendAjax(param, function(result) {
        submitRecord('edit', '认证页-上传资料', '商家认证', getValue('infoid'));
        parent.closeSlipIframeDiv();
        parent.queryKey('page');
    });
}

function getFieldAndTable() {
    var result = '';
    var infoType = getValue('infoType');
    if (infoType == 'electronic') {
        result = 'electronic@electronicid';
    }
    else if (infoType == 'housecraft') {
        result = 'housecraft@housecraftid';
    }
    else if (infoType == 'other') {
        result = 'other@otherid';
    }

    return result;
}